package cn.liyicun.cp.cpweb.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.liyicun.cp.cpweb.entity.ExpectInfo;
import cn.liyicun.cp.cpweb.entity.ExpectInfoExample;
import cn.liyicun.cp.cpweb.mapper.ExpectInfoMapper;
import cn.liyicun.cp.cpweb.mapper.ext.ExtExpectInfoMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ExpectInfoService {

    @Autowired
    private ExpectInfoMapper expectInfoMapper;
    @Autowired
    private ExtExpectInfoMapper extExpectInfoMapper;

    @Autowired
    SettingInfoService settingInfoService;

    public void task(String code) {

        Map<String, String> map = settingInfoService.mapCache("cp_expect_init");


        Boolean bjpk10 = MapUtil.getBool(map, code);
        if (bjpk10 != null && bjpk10) {

            ExpectInfo info = maxExpectInfoByCode(code);
            if (info != null) {

                initExpect(code, info.getExpect(), info.getEndTime(),
                        MapUtil.getStr(map, "bjpk10_timeSet"),
                        MapUtil.getInt(map, "bjpk10_interval"),
                        MapUtil.getInt(map, "bjpk10_size"));
            }
        }

    }

    public void initExpect(String code, Long expect, Date time, String timeSet, int interval, int size) {
//        DateTime time = DateUtil.parse(times, "yyyy-MM-dd HH:mm:ss");

        // 712419	2018-11-01 11:47:45
//        int interval = 5;
//        String timeSet = "09:00";

        int a = 0;
        for (int i = 0; i < size; i++) {
            DateTime datetime = DateUtil.offsetMinute(time, i * interval);
            String hm = datetime.toString("HH:mm");
            //System.out.print("09:01".compareTo(hm)+"\t"+hm+"\t");
            if (timeSet.compareTo(hm) <= -1) {
                Long expectTemp = a + expect;
                saveExpect(code, expectTemp, datetime);
                a++;
            }
        }
    }

    private void saveExpect(String code, Long expect, Date endDate) {

        ExpectInfo info = getExpectInfo(code, expect);
        if (info == null) {
            ExpectInfo record = new ExpectInfo();
            record.setCreateTime(new Date());
            record.setEndTime(endDate);
            record.setExpect(expect);
            record.setCode(code);
            record.setOpenStatus(0);
            expectInfoMapper.insertSelective(record);
        }
    }

    public ExpectInfo getExpectInfo(String code, Long expect) {
        ExpectInfo key = new ExpectInfo();
        key.setExpect(expect);
        key.setCode(code);
        return expectInfoMapper.selectByPrimaryKey(key);
    }

    private ExpectInfo maxExpectInfoByCode(String code) {
        ExpectInfoExample exa = new ExpectInfoExample();
        exa.createCriteria().andCodeEqualTo(code);
        exa.setOrderByClause("expect desc");
        RowBounds row = new RowBounds(0, 1);
        List<ExpectInfo> list = expectInfoMapper.selectByExampleWithRowbounds(exa, row);
        return list.isEmpty() ? null : list.get(0);
    }


    public void openPrize(String code, Long expect, String openCode, Date openDate) {
        ExpectInfo info = getExpectInfo(code, expect);
        if (info == null) {
            return;
        }
        if (info.getOpenStatus() == 2) {
            return;
        }


        info.setOpenStatus(2);
        info.setOpenCode(openCode);
        info.setOpenTime(openDate);
        info.setUpdateTime(new Date());
        expectInfoMapper.updateByPrimaryKeySelective(info);
    }


    public ExpectInfo nextExpect() {
        ExpectInfo expect = extExpectInfoMapper.nextExpect();
        return expect;
    }


    public List<ExpectInfo> listLatelyExpect(int limit) {
        List<ExpectInfo> list = extExpectInfoMapper.listLatelyExpect(limit);
        return list;
    }



}
